package com.example.borui.partpositionproject.simple.partposition.one;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.borui.partpositionproject.partpisition.base.BasePartPosition;
import com.example.borui.partpositionproject.partpisition.base.IPartPositionView;
import com.example.borui.partpositionproject.simple.bean.DataBean;
import com.example.borui.partpositionproject.simple.partposition.IViewType;
import com.example.borui.partpositionproject.simple.partposition.divider.DividerLineViewHolder;
import com.example.borui.partpositionproject.simple.partposition.widget.OneView;

/**
 * Created by borui on 2017/9/29.
 */
public class OnePartPosition extends BasePartPosition<DataBean> {

    private int divide_type = 1;
    private int normal_type = 2;

    private boolean isHasDivider = true;

    public OnePartPosition(Context mContext, int type, int position, IPartPositionView view) {
        super(mContext, type, position, view);
        divide_type = initItemViewType(getClass().toString() + divide_type);
        normal_type = initItemViewType(getClass().toString() + normal_type);
    }

    @Override
    public RecyclerView.ViewHolder createViewHolder(int viewType) {
        if(viewType == normal_type){
            OneView oneView = new OneView(getContext());
            return new OneViewHolder(oneView);
        }else if(viewType == divide_type){
            return new DividerLineViewHolder(new View(getContext()));
        }
        return new OneViewHolder(new OneView(getContext()));
    }

    @Override
    public int getItemViewType(int position) {
        if(position == getList().size() - 1){
            return divide_type;
        }
        return normal_type;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(null != holder && holder instanceof OneViewHolder){
            OneViewHolder oneViewHolder = (OneViewHolder) holder;
            if(null != oneViewHolder.oneView){
                oneViewHolder.oneView.initData(getData(), getData().getTextContent());
            }
        }

        if(null != holder && holder instanceof DividerLineViewHolder){
            DividerLineViewHolder dividerLineViewHolder = (DividerLineViewHolder) holder;

            if(isHasDivider){
                dividerLineViewHolder.init();
            }else{
                dividerLineViewHolder.init(0);
            }
        }
    }

    @Override
    protected void createList() {
        mList.clear();
        mList.add(getAreaPosition());
        mList.add(getAreaPosition());
    }

    public void setHasDivider(boolean hasDivider) {
        isHasDivider = hasDivider;
    }

    class OneViewHolder extends RecyclerView.ViewHolder{

        OneView oneView;

        public OneViewHolder(View itemView) {
            super(itemView);
            if(null != itemView && itemView instanceof OneView){
                oneView = (OneView) itemView;
            }
        }
    }
}
